package com.ccl.iot.views;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ccl.iot.R;
import com.ccl.iot.asynctask.SendIrTask;
import com.ccl.iot.asynctask.StudyIrTask;
import com.ccl.iot.batch.IOTBatchInterface;
import com.ccl.iot.bean.CodeviewBean;
import com.ccl.iot.device.DeviceIconProvider;
import com.ccl.iot.device.IOTBLir;
import com.ccl.iot.device.IOTSubBLir;
import com.ccl.iot.dvr.utils.GlideUtil;
import com.ccl.iot.ir.BlirConst;
import com.ccl.iot.publics.UIUtils;
import com.hzy.tvmao.KookongSDK;

import java.util.HashMap;

import cn.com.broadlink.sdk.BLLet;
import cn.com.broadlink.sdk.constants.controller.BLDeviceState;


public class SubBlirControlView extends LinearLayout {

    public ImageView mIvBigImage;
    private CheckableImageView mIvDvdPlay;
    private CheckableImageView mIvDvdPause;
    private CheckableImageView mIvDvdStop;
    private CheckableImageView mIvControlMore;
    private CheckableImageView mIvControlTop;
    private CheckableImageView mIvControlSound;
    private CheckableImageView mIvControlLeft;
    private CheckableTextView mIvControlOk;
    private CheckableImageView mIvControlRight;
    private CheckableImageView mIvControlNumber;
    private CheckableImageView mIvControlBottom;
    private CheckableImageView mIvControlReturn;
    private CheckableImageView mIvControlToggle;
    private LinearLayout mLlDvdControl;

    private IOTSubBLir mSubBLir;
    private View mOfflineMask;
    private CheckableTextView mNumb0;
    private CheckableTextView mNumb1;
    private CheckableTextView mNumb2;
    private CheckableTextView mNumb3;
    private CheckableTextView mNumb4;
    private CheckableTextView mNumb5;
    private CheckableTextView mNumb6;
    private CheckableTextView mNumb7;
    private CheckableTextView mNumb8;
    private CheckableTextView mNumb9;
    private CheckableTextView mNumbOther;
    private ImageView mNumbPopBack;

    private PopupWindow mPopupWindow;
    private View mPopView;

    private boolean mCheckable;

    private IOTBatchInterface.IOTBlirViewCheckInterface mLastCheckView;
    private HashMap<String, IOTBatchInterface.IOTBlirViewCheckInterface> mBlIrViews;
    private CustomBlirContrlView mCustomSubDevView;
    private IrAirControlView mIrAirControlView;
    private LinearLayout mLlNormalDevView;
    private LinearLayout mLlNormalDevControlView;
    private boolean mCheckOnline;

    public LinearLayout mCameraLayout;

    private int mPadding = 0;
    private Context mContext;

    public int mDeviceImgHeight;
    public int mDeviceImgWidht;
    private LinearLayout mLeftPart;
    private LinearLayout mRightPart;
    private LinearLayout mHeartBottomPart;
    private CheckableImageView mLeftPartTop;
    private CheckableImageView mLeftPartBottom;
    private CheckableImageView mRightPartTop;
    private CheckableImageView mRightPartBottom;
    private CheckableImageView mHeartPartLeft;
    private CheckableImageView mHeartPartRight;
    private TextView mRightPartTv;
    private CheckableLinearLayout mFanTiming;
    private LinearLayout mFanContainer;
    private CheckableLinearLayout mFanHead;
    private CheckableLinearLayout mFanDamper;
    private CheckableLinearLayout mFanMode;

    public SubBlirControlView(Context context) {
        this(context, null);
    }

    public SubBlirControlView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SubBlirControlView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        init();
        initListener();
        initData();
    }

    public void setCheckable(boolean checkable) {
        mCheckable = checkable;

        mIvControlToggle.setCheckable(mCheckable);
        mIvControlMore.setCheckable(mCheckable);
        mIvControlSound.setCheckable(mCheckable);
        mIvControlOk.setCheckable(mCheckable);
        mIvControlReturn.setCheckable(mCheckable);
        mIvDvdPlay.setCheckable(mCheckable);
        mIvDvdPause.setCheckable(mCheckable);
        mIvDvdStop.setCheckable(mCheckable);
        mIvControlTop.setCheckable(mCheckable);
        mIvControlLeft.setCheckable(mCheckable);
        mIvControlRight.setCheckable(mCheckable);
        mIvControlBottom.setCheckable(mCheckable);
        mIvControlNumber.setCheckable(mCheckable);

        mNumb0.setCheckable(mCheckable);
        mNumb1.setCheckable(mCheckable);
        mNumb2.setCheckable(mCheckable);
        mNumb3.setCheckable(mCheckable);
        mNumb4.setCheckable(mCheckable);
        mNumb5.setCheckable(mCheckable);
        mNumb6.setCheckable(mCheckable);
        mNumb7.setCheckable(mCheckable);
        mNumb8.setCheckable(mCheckable);
        mNumb9.setCheckable(mCheckable);
        mNumbOther.setCheckable(mCheckable);
        mLeftPartTop.setCheckable(mCheckable);
        mLeftPartBottom.setCheckable(mCheckable);
        mRightPartTop.setCheckable(mCheckable);
        mRightPartBottom.setCheckable(mCheckable);
        mHeartPartLeft.setCheckable(mCheckable);
        mHeartPartRight.setCheckable(mCheckable);

        mFanTiming.setCheckable(mCheckable);
        mFanHead.setCheckable(mCheckable);
        mFanDamper.setCheckable(mCheckable);
        mFanMode.setCheckable(mCheckable);

        if (mCheckable) {
            mIvControlToggle.setOnBlirViewChected(mOnBlirViewChecked);
            mIvControlMore.setOnBlirViewChected(mOnBlirViewChecked);
            mIvControlSound.setOnBlirViewChected(mOnBlirViewChecked);
            mIvControlOk.setOnBlirViewChected(mOnBlirViewChecked);
            mIvControlReturn.setOnBlirViewChected(mOnBlirViewChecked);
            mIvDvdPlay.setOnBlirViewChected(mOnBlirViewChecked);
            mIvDvdPause.setOnBlirViewChected(mOnBlirViewChecked);
            mIvDvdStop.setOnBlirViewChected(mOnBlirViewChecked);
            mIvControlTop.setOnBlirViewChected(mOnBlirViewChecked);
            mIvControlLeft.setOnBlirViewChected(mOnBlirViewChecked);
            mIvControlRight.setOnBlirViewChected(mOnBlirViewChecked);
            mIvControlBottom.setOnBlirViewChected(mOnBlirViewChecked);
            mIvControlNumber.setOnBlirViewChected(mOnBlirViewChecked);
            mNumb0.setOnBlirViewChected(mOnBlirViewChecked);
            mNumb1.setOnBlirViewChected(mOnBlirViewChecked);
            mNumb2.setOnBlirViewChected(mOnBlirViewChecked);
            mNumb3.setOnBlirViewChected(mOnBlirViewChecked);
            mNumb4.setOnBlirViewChected(mOnBlirViewChecked);
            mNumb5.setOnBlirViewChected(mOnBlirViewChecked);
            mNumb6.setOnBlirViewChected(mOnBlirViewChecked);
            mNumb7.setOnBlirViewChected(mOnBlirViewChecked);
            mNumb8.setOnBlirViewChected(mOnBlirViewChecked);
            mNumb9.setOnBlirViewChected(mOnBlirViewChecked);
            mNumbOther.setOnBlirViewChected(mOnBlirViewChecked);

            mLeftPartTop.setOnBlirViewChected(mOnBlirViewChecked);
            mLeftPartBottom.setOnBlirViewChected(mOnBlirViewChecked);
            mRightPartTop.setOnBlirViewChected(mOnBlirViewChecked);
            mRightPartBottom.setOnBlirViewChected(mOnBlirViewChecked);
            mHeartPartLeft.setOnBlirViewChected(mOnBlirViewChecked);
            mHeartPartRight.setOnBlirViewChected(mOnBlirViewChecked);

            mFanTiming.setOnBlirViewChected(mOnBlirViewChecked);
            mFanHead.setOnBlirViewChected(mOnBlirViewChecked);
            mFanDamper.setOnBlirViewChected(mOnBlirViewChecked);
            mFanMode.setOnBlirViewChected(mOnBlirViewChecked);

            mBlIrViews = new HashMap<>();
            mBlIrViews.put((String) mIvControlToggle.getTag(), mIvControlToggle);
            mBlIrViews.put((String) mIvControlMore.getTag(), mIvControlMore);
            mBlIrViews.put((String) mIvControlSound.getTag(), mIvControlSound);
            mBlIrViews.put((String) mIvControlOk.getTag(), mIvControlOk);
            mBlIrViews.put((String) mIvControlReturn.getTag(), mIvControlReturn);
            mBlIrViews.put((String) mIvDvdPlay.getTag(), mIvDvdPlay);
            mBlIrViews.put((String) mIvDvdPause.getTag(), mIvDvdPause);
            mBlIrViews.put((String) mIvDvdStop.getTag(), mIvDvdStop);
            mBlIrViews.put((String) mIvControlTop.getTag(), mIvControlTop);
            mBlIrViews.put((String) mIvControlLeft.getTag(), mIvControlLeft);
            mBlIrViews.put((String) mIvControlRight.getTag(), mIvControlRight);
            mBlIrViews.put((String) mIvControlBottom.getTag(), mIvControlBottom);
            mBlIrViews.put((String) mNumb0.getTag(), mNumb0);
            mBlIrViews.put((String) mNumb1.getTag(), mNumb1);
            mBlIrViews.put((String) mNumb2.getTag(), mNumb2);
            mBlIrViews.put((String) mNumb3.getTag(), mNumb3);
            mBlIrViews.put((String) mNumb4.getTag(), mNumb4);
            mBlIrViews.put((String) mNumb5.getTag(), mNumb5);
            mBlIrViews.put((String) mNumb6.getTag(), mNumb6);
            mBlIrViews.put((String) mNumb7.getTag(), mNumb7);
            mBlIrViews.put((String) mNumb8.getTag(), mNumb8);
            mBlIrViews.put((String) mNumb9.getTag(), mNumb9);
            mBlIrViews.put((String) mNumbOther.getTag(), mNumbOther);

            mBlIrViews.put((String) mLeftPartTop.getTag(), mLeftPartTop);
            mBlIrViews.put((String) mLeftPartBottom.getTag(), mLeftPartBottom);
            mBlIrViews.put((String) mRightPartTop.getTag(), mRightPartTop);
            mBlIrViews.put((String) mRightPartBottom.getTag(), mRightPartBottom);
            mBlIrViews.put((String) mHeartPartLeft.getTag(), mHeartPartLeft);
            mBlIrViews.put((String) mHeartPartRight.getTag(), mHeartPartRight);
            mBlIrViews.put(BlirConst.IR_KEYTYPE_ZOOMIN, mRightPartTop);
            mBlIrViews.put(BlirConst.IR_KEYTYPE_ZOOMOUT, mRightPartBottom);

            mBlIrViews.put(BlirConst.IR_KEYTYPE_BACK, mIvControlTop);
            mBlIrViews.put(BlirConst.IR_KEYTYPE_BACKWARD, mIvControlLeft);
            mBlIrViews.put(BlirConst.IR_KEYTYPE_FORWARD, mIvControlRight);
            mBlIrViews.put(BlirConst.IR_KEYTYPE_NEXT, mIvControlBottom);
            mBlIrViews.put(BlirConst.IR_KEYTYPE_OPEN, mIvControlNumber);

            mBlIrViews.put((String) mFanTiming.getTag(), mFanTiming);
            mBlIrViews.put((String) mFanHead.getTag(), mFanHead);
            mBlIrViews.put((String) mFanDamper.getTag(), mFanDamper);
            mBlIrViews.put((String) mFanMode.getTag(), mFanMode);

        }
    }

    private IOTBatchInterface.OnBlirViewChecked mOnBlirViewChecked = new IOTBatchInterface.OnBlirViewChecked() {
        @Override
        public void onChected(boolean checked, String tag, IOTBatchInterface.IOTBlirViewCheckInterface view) {
            if (checked) {
                if (mLastCheckView != null) {
                    mLastCheckView.setChecked(false);
                    mLastCheckView = null;
                }
                mSubBLir.setBatchCodes(tag);
                mLastCheckView = view;
            } else {
                mSubBLir.setBatchCodes("");
                mLastCheckView = null;
            }
        }
    };

    private IOTSubBLir.OnCheckedClearListener mCheckedClearListener = new IOTSubBLir.OnCheckedClearListener() {
        @Override
        public void onCheckedClear() {
            if (mLastCheckView != null) {
                mLastCheckView.setChecked(false);
                mLastCheckView = null;
            }
        }
    };

    private IOTBLir.OnCodeChangedListener mCodeChangedListener = new IOTBLir.OnCodeChangedListener() {
        @Override
        public void onCodeChanged(IOTSubBLir subBLir, CodeviewBean bean, boolean isNew) {
            final String devType = mSubBLir.getDevType();
            UIUtils.postTaskSafely(new Runnable() {
                @Override
                public void run() {
                    if (BlirConst.IR_SUBDEV_TYPE_OTHER.equals(devType)) {
                        mCustomSubDevView.setSubDev(mSubBLir, mOnlineChangeListener, mOfflineMask, mCheckable);
                    } else if (BlirConst.IR_SUBDEV_TYPE_AIRCONDITION.equals(devType)) {
                        mIrAirControlView.setSubDev(mSubBLir, mOnlineChangeListener, mOfflineMask, mIvBigImage, mCheckable);
                    } else {
                        updateViewSelected(mSubBLir);
                        updateNumberSelected(mSubBLir);
                    }
                }
            });
        }

        @Override
        public void onSubDevDelete(IOTSubBLir subBLir) {

        }
    };

    public interface OnDevOnlineChangeListener {
        public void onDevOnlineChange();
    }

    private OnDevOnlineChangeListener mOnlineChangeListener;

    public void setOnlineChangeListener(OnDevOnlineChangeListener l) {
        mOnlineChangeListener = l;
    }

    private void init() {
        View.inflate(getContext(), R.layout.view_subblir_control, this);
        mIvBigImage = (ImageView) findViewById(R.id.iv_big_iamge);
        mLlDvdControl = (LinearLayout) findViewById(R.id.ll_dvd_control);
        mOfflineMask = findViewById(R.id.view_offline_mask);
        mCustomSubDevView = (CustomBlirContrlView) findViewById(R.id.gv_custom_irdev_view);
        mIrAirControlView = (IrAirControlView) findViewById(R.id.ir_air_control_view);
        mLlNormalDevView = (LinearLayout) findViewById(R.id.ll_normal_blir_continar);
        mLlNormalDevControlView = (LinearLayout) findViewById(R.id.ll_normal_contral_container);

        mIvDvdPlay = (CheckableImageView) findViewById(R.id.iv_dvd_play);
        mIvDvdPause = (CheckableImageView) findViewById(R.id.iv_dvd_pause);
        mIvDvdStop = (CheckableImageView) findViewById(R.id.iv_dvd_stop);
        mIvControlMore = (CheckableImageView) findViewById(R.id.iv_control_more);
        mIvControlTop = (CheckableImageView) findViewById(R.id.iv_control_top);
        mIvControlSound = (CheckableImageView) findViewById(R.id.iv_control_sound);
        mIvControlLeft = (CheckableImageView) findViewById(R.id.iv_control_left);
        mIvControlOk = (CheckableTextView) findViewById(R.id.iv_control_ok);
        mIvControlRight = (CheckableImageView) findViewById(R.id.iv_control_right);
        mIvControlNumber = (CheckableImageView) findViewById(R.id.iv_control_number);
        mIvControlBottom = (CheckableImageView) findViewById(R.id.iv_control_bottom);
        mIvControlReturn = (CheckableImageView) findViewById(R.id.iv_control_return);
        mIvControlToggle = (CheckableImageView) findViewById(R.id.iv_control_toggle);

        mCameraLayout = (LinearLayout) findViewById(R.id.layout_camera_frame);

        mPopView = View.inflate(getContext(), R.layout.pop_ir_number_view, null);

        mNumb0 = (CheckableTextView) mPopView.findViewById(R.id.tv_pop_number_0);
        mNumb1 = (CheckableTextView) mPopView.findViewById(R.id.tv_pop_number_1);
        mNumb2 = (CheckableTextView) mPopView.findViewById(R.id.tv_pop_number_2);
        mNumb3 = (CheckableTextView) mPopView.findViewById(R.id.tv_pop_number_3);
        mNumb4 = (CheckableTextView) mPopView.findViewById(R.id.tv_pop_number_4);
        mNumb5 = (CheckableTextView) mPopView.findViewById(R.id.tv_pop_number_5);
        mNumb6 = (CheckableTextView) mPopView.findViewById(R.id.tv_pop_number_6);
        mNumb7 = (CheckableTextView) mPopView.findViewById(R.id.tv_pop_number_7);
        mNumb8 = (CheckableTextView) mPopView.findViewById(R.id.tv_pop_number_8);
        mNumb9 = (CheckableTextView) mPopView.findViewById(R.id.tv_pop_number_9);
        mNumbOther = (CheckableTextView) mPopView.findViewById(R.id.tv_pop_number_outher);
        mNumbPopBack = (ImageView) mPopView.findViewById(R.id.iv_pop_back);

        mLeftPart = (LinearLayout) findViewById(R.id.ll_left_part);
        mRightPart = (LinearLayout) findViewById(R.id.ll_right_part);
        mRightPartTv = (TextView) findViewById(R.id.tv_right_part);
        mHeartBottomPart = (LinearLayout) findViewById(R.id.ll_heater_bottom_part);
        mLeftPartTop = (CheckableImageView) findViewById(R.id.iv_left_part_top);
        mLeftPartBottom = (CheckableImageView) findViewById(R.id.iv_left_part_bottom);
        mRightPartTop = (CheckableImageView) findViewById(R.id.iv_right_part_top);
        mRightPartBottom = (CheckableImageView) findViewById(R.id.iv_right_part_bottom);
        mHeartPartLeft = (CheckableImageView) findViewById(R.id.iv_heater_part_left);
        mHeartPartRight = (CheckableImageView) findViewById(R.id.iv_heater_part_right);

        //风扇
        mFanContainer = (LinearLayout) findViewById(R.id.ll_fan_container);
        mFanTiming = (CheckableLinearLayout) findViewById(R.id.cll_fan_timing);
        mFanHead = (CheckableLinearLayout) findViewById(R.id.cll_fan_head);
        mFanDamper = (CheckableLinearLayout) findViewById(R.id.cll_fan_damper);
        mFanMode = (CheckableLinearLayout) findViewById(R.id.cll_fan_swingmode);

    }

    private void initListener() {
        mIvDvdPlay.setOnClickListener(mOnClickListener);
        mIvDvdPause.setOnClickListener(mOnClickListener);
        mIvDvdStop.setOnClickListener(mOnClickListener);
        mIvControlMore.setOnClickListener(mOnClickListener);
        mIvControlTop.setOnClickListener(mOnClickListener);
        mIvControlSound.setOnClickListener(mOnClickListener);
        mIvControlLeft.setOnClickListener(mOnClickListener);
        mIvControlOk.setOnClickListener(mOnClickListener);
        mIvControlRight.setOnClickListener(mOnClickListener);
        mIvControlNumber.setOnClickListener(mOnClickListener);
        mIvControlBottom.setOnClickListener(mOnClickListener);
        mIvControlReturn.setOnClickListener(mOnClickListener);
        mIvControlToggle.setOnClickListener(mOnClickListener);

        mNumb0.setOnClickListener(mOnClickListener);
        mNumb1.setOnClickListener(mOnClickListener);
        mNumb2.setOnClickListener(mOnClickListener);
        mNumb3.setOnClickListener(mOnClickListener);
        mNumb4.setOnClickListener(mOnClickListener);
        mNumb5.setOnClickListener(mOnClickListener);
        mNumb6.setOnClickListener(mOnClickListener);
        mNumb7.setOnClickListener(mOnClickListener);
        mNumb8.setOnClickListener(mOnClickListener);
        mNumb9.setOnClickListener(mOnClickListener);
        mNumbOther.setOnClickListener(mOnClickListener);
        mNumbPopBack.setOnClickListener(mOnClickListener);

        mLeftPartTop.setOnClickListener(mOnClickListener);
        mLeftPartBottom.setOnClickListener(mOnClickListener);
        mRightPartTop.setOnClickListener(mOnClickListener);
        mRightPartBottom.setOnClickListener(mOnClickListener);
        mHeartPartLeft.setOnClickListener(mOnClickListener);
        mHeartPartRight.setOnClickListener(mOnClickListener);

        mFanTiming.setOnClickListener(mOnClickListener);
        mFanHead.setOnClickListener(mOnClickListener);
        mFanDamper.setOnClickListener(mOnClickListener);
        mFanMode.setOnClickListener(mOnClickListener);


        mIvDvdPlay.setOnLongClickListener(mOnLongClickListener);
        mIvDvdPause.setOnLongClickListener(mOnLongClickListener);
        mIvDvdStop.setOnLongClickListener(mOnLongClickListener);
        mIvControlMore.setOnLongClickListener(mOnLongClickListener);
        mIvControlTop.setOnLongClickListener(mOnLongClickListener);
        mIvControlSound.setOnLongClickListener(mOnLongClickListener);
        mIvControlLeft.setOnLongClickListener(mOnLongClickListener);
        mIvControlOk.setOnLongClickListener(mOnLongClickListener);
        mIvControlRight.setOnLongClickListener(mOnLongClickListener);
        mIvControlNumber.setOnLongClickListener(mOnLongClickListener);
        mIvControlReturn.setOnLongClickListener(mOnLongClickListener);
        mIvControlBottom.setOnLongClickListener(mOnLongClickListener);
        mIvControlToggle.setOnLongClickListener(mOnLongClickListener);

        mNumb0.setOnLongClickListener(mOnLongClickListener);
        mNumb1.setOnLongClickListener(mOnLongClickListener);
        mNumb2.setOnLongClickListener(mOnLongClickListener);
        mNumb3.setOnLongClickListener(mOnLongClickListener);
        mNumb4.setOnLongClickListener(mOnLongClickListener);
        mNumb5.setOnLongClickListener(mOnLongClickListener);
        mNumb6.setOnLongClickListener(mOnLongClickListener);
        mNumb7.setOnLongClickListener(mOnLongClickListener);
        mNumb8.setOnLongClickListener(mOnLongClickListener);
        mNumb9.setOnLongClickListener(mOnLongClickListener);
        mNumbOther.setOnLongClickListener(mOnLongClickListener);

        mLeftPartTop.setOnLongClickListener(mOnLongClickListener);
        mLeftPartBottom.setOnLongClickListener(mOnLongClickListener);
        mRightPartTop.setOnLongClickListener(mOnLongClickListener);
        mRightPartBottom.setOnLongClickListener(mOnLongClickListener);
        mHeartPartLeft.setOnLongClickListener(mOnLongClickListener);
        mHeartPartRight.setOnLongClickListener(mOnLongClickListener);

        mFanTiming.setOnLongClickListener(mOnLongClickListener);
        mFanHead.setOnLongClickListener(mOnLongClickListener);
        mFanDamper.setOnLongClickListener(mOnLongClickListener);
        mFanMode.setOnLongClickListener(mOnLongClickListener);
    }

    public static final String KK_APP_KEY = "6835C8A3C30C916683E66C81449B6438";
    public static long KK_irDeviceId = 0;//如果是按红外设备授权收费则填上设备的id，否则使用KookongSDK.init(this,APP_KEY);

    private void initData() {
        mIvControlToggle.setTag(BlirConst.IR_KEYTYPE_POWER);
        mIvControlMore.setTag(BlirConst.IR_KEYTYPE_MENU);
        mIvControlSound.setTag(BlirConst.IR_KEYTYPE_MUTE);
        mIvControlOk.setTag(BlirConst.IR_KEYTYPE_OK);
        mIvControlReturn.setTag(BlirConst.IR_KEYTYPE_RETURN);
        mIvDvdPlay.setTag(BlirConst.IR_KEYTYPE_PLAY);
        mIvDvdPause.setTag(BlirConst.IR_KEYTYPE_PAUSE);
        mIvDvdStop.setTag(BlirConst.IR_KEYTYPE_STOP);

        mLlDvdControl.setVisibility(View.GONE);
        mIvControlTop.setTag(BlirConst.IR_KEYTYPE_UP);
        mIvControlLeft.setTag(BlirConst.IR_KEYTYPE_LEFT);
        mIvControlRight.setTag(BlirConst.IR_KEYTYPE_RIGHT);
        mIvControlBottom.setTag(BlirConst.IR_KEYTYPE_DOWN);
        mIvControlNumber.setTag(null);
        mIvControlNumber.setSelected(false);

        mNumb0.setTag(BlirConst.IR_KEYTYPE_KEY0);
        mNumb1.setTag(BlirConst.IR_KEYTYPE_KEY1);
        mNumb2.setTag(BlirConst.IR_KEYTYPE_KEY2);
        mNumb3.setTag(BlirConst.IR_KEYTYPE_KEY3);
        mNumb4.setTag(BlirConst.IR_KEYTYPE_KEY4);
        mNumb5.setTag(BlirConst.IR_KEYTYPE_KEY5);
        mNumb6.setTag(BlirConst.IR_KEYTYPE_KEY6);
        mNumb7.setTag(BlirConst.IR_KEYTYPE_KEY7);
        mNumb8.setTag(BlirConst.IR_KEYTYPE_KEY8);
        mNumb9.setTag(BlirConst.IR_KEYTYPE_KEY9);
        mNumbOther.setTag(BlirConst.IR_KEYTYPE_other);

        mLeftPartTop.setTag(BlirConst.IR_KEYTYPE_VOL_ADD);
        mLeftPartBottom.setTag(BlirConst.IR_KEYTYPE_VOL_RDEDUCE);
        mRightPartTop.setTag(BlirConst.IR_KEYTYPE_CH_ADD);
        mRightPartBottom.setTag(BlirConst.IR_KEYTYPE_CH_RDEDUCE);
        mHeartPartLeft.setTag(BlirConst.IR_KEYTYPE_KEY0);
        mHeartPartRight.setTag(BlirConst.IR_KEYTYPE_KEY0);

        mFanTiming.setTag(BlirConst.IR_TIMING);
        mFanHead.setTag(BlirConst.IR_FAN_HEAD);
        mFanDamper.setTag(BlirConst.IR_FAN_SPEED);
        mFanMode.setTag(BlirConst.IR_FAN_MODE);

        boolean result = KookongSDK.init(getContext(), KK_APP_KEY, KK_irDeviceId + "");
        Log.e("KKR", "Verify result is " + result + ", KK_irDeviceId: " + KK_irDeviceId);
        KookongSDK.setDebugMode(true);
    }

    private OnClickListener mOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {

            if (v.getId() == R.id.iv_control_number && !BlirConst.IR_SUBDEV_TYPE_PLAYER.equals(mSubBLir.getDevType())) {
                if (mPopupWindow == null) {
                    //创建PopupWindow对象，指定宽度和高度
                    mPopupWindow = new PopupWindow(mPopView, ViewGroup.LayoutParams.MATCH_PARENT, UIUtils.dp2px(300));

                    //设置动画
                    mPopupWindow.setAnimationStyle(R.style.take_photo_anim);
                    //设置背景颜色
                    mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    //设置可以获取焦点
                    mPopupWindow.setFocusable(true);
                    //设置可以触摸弹出框以外的区域
                    mPopupWindow.setOutsideTouchable(true);
                }
                mPopupWindow.showAtLocation(SubBlirControlView.this, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                updateNumberSelected(mSubBLir);

            } else if (v.getId() == R.id.iv_pop_back) {
                if (mPopupWindow != null && mPopupWindow.isShowing()) {
                    mPopupWindow.dismiss();
                }
            } else {
                if (!v.isSelected()) {
                    String tag = (String) v.getTag();
                    if (tag != null) {
                        new SendIrTask(getContext(), mSubBLir, mOnlineChangeListener, mOfflineMask).execute(tag);
                    }
                }
            }

        }
    };


    private OnLongClickListener mOnLongClickListener = new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (!mCheckOnline) {
                String tag = (String) v.getTag();
                if (tag != null) {
                    new StudyIrTask(getContext(), mSubBLir, mOnlineChangeListener, mOfflineMask).execute(tag, null);
                }
            }
            return true;
        }
    };

    /**
     * 设置子设备
     *
     * @param dev         子设备
     * @param checkOnline 是否需要检查在线状态(在设备界面就不需要检查, 在意向控制界面就需要检查) true:在意向控制页
     */
    public void setIrSubDev(IOTSubBLir dev, boolean checkOnline) {
        mCheckOnline = checkOnline;
        if (dev != null) {
            String devType = dev.getDevType();
            dev.setOnCheckedClearListener(mCheckedClearListener);

            updateDevImage(dev, devType);

            if (BlirConst.IR_SUBDEV_TYPE_OTHER.equals(devType) || BlirConst.IR_SUBDEV_TYPE_NON_STATE_AIRCONDITION.equals(devType)) {
                if (mCustomSubDevView.getVisibility() != View.VISIBLE) {
                    mCustomSubDevView.setVisibility(View.VISIBLE);
                }
                if (mLlNormalDevView.getVisibility() != View.GONE) {
                    mLlNormalDevView.setVisibility(View.GONE);
                }
                if (mIrAirControlView.getVisibility() != View.GONE) {
                    mIrAirControlView.setVisibility(View.GONE);
                }
                if (mLlDvdControl.getVisibility() != View.GONE) {
                    mLlDvdControl.setVisibility(View.GONE);
                }
                if (mLeftPart.getVisibility() != View.GONE) {
                    mLeftPart.setVisibility(View.GONE);
                }
                if (mRightPart.getVisibility() != View.GONE) {
                    mRightPart.setVisibility(View.GONE);
                }
                if (mHeartBottomPart.getVisibility() != View.GONE) {
                    mHeartBottomPart.setVisibility(View.GONE);
                }
                mCustomSubDevView.setSubDev(dev, mOnlineChangeListener, mOfflineMask, mCheckable);
            } else if (BlirConst.IR_SUBDEV_TYPE_AIRCONDITION.equals(devType)) {
                if (mCustomSubDevView.getVisibility() != View.GONE) {
                    mCustomSubDevView.setVisibility(View.GONE);
                }
                if (mLlNormalDevView.getVisibility() != View.GONE) {
                    mLlNormalDevView.setVisibility(View.GONE);
                }
                if (mLlDvdControl.getVisibility() != View.GONE) {
                    mLlDvdControl.setVisibility(View.GONE);
                }
                if (mIrAirControlView.getVisibility() != View.VISIBLE) {
                    mIrAirControlView.setVisibility(View.VISIBLE);
                }
                if (mLeftPart.getVisibility() != View.GONE) {
                    mLeftPart.setVisibility(View.GONE);
                }
                if (mRightPart.getVisibility() != View.GONE) {
                    mRightPart.setVisibility(View.GONE);
                }
                if (mHeartBottomPart.getVisibility() != View.GONE) {
                    mHeartBottomPart.setVisibility(View.GONE);
                }

                mIrAirControlView.setSubDev(dev, mOnlineChangeListener, mOfflineMask, mIvBigImage, mCheckable);
            } else {
                if (mCustomSubDevView.getVisibility() != View.GONE) {
                    mCustomSubDevView.setVisibility(View.GONE);
                }
                if (mIrAirControlView.getVisibility() != View.GONE) {
                    mIrAirControlView.setVisibility(View.GONE);
                }
                if (mLlNormalDevView.getVisibility() != View.VISIBLE) {
                    mLlNormalDevView.setVisibility(View.VISIBLE);
                }
                updateViewSelected(dev);
                updateNumberSelected(dev);
            }
            if (checkOnline) {
                if (mSubBLir != null) {
                    mSubBLir.setOnCodeChangedListener(null);
                    mSubBLir.setOnCheckedClearListener(null);
                }
                dev.setOnCodeChangedListener(mCodeChangedListener);
            }

            mSubBLir = dev;
            if (mCheckable) {
                checkDev2Batch();
            }
            if (checkOnline) {
                checkOnline();
            }
        }
    }


    public void updateDevImage(IOTSubBLir dev, String devType) {


        //        mIvBigImage.setImageResource(DeviceIconProvider.getBlirNormalId(devType));
        if (dev.getImageCount() == 0) {
            //没拍过照的
            int imageID = DeviceIconProvider.getBlirNormalId(devType);
            mIvBigImage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            GlideUtil.loadImageView(mContext, imageID, mIvBigImage);

        } else {
            mIvBigImage.setScaleType(ImageView.ScaleType.CENTER);
            String imagePath = dev.getImagePath();
            GlideUtil.loadImageViewNoCacheDisk(mContext, imagePath, mIvBigImage);
        }

    }

    /**
     * 拍照后用
     *
     * @param dev
     */
    public void updateDevImage(IOTSubBLir dev) {
        if (dev != null) {
            String devType = dev.getDevType();

            updateDevImage(dev, devType);

        }
    }

    public void checkDev2Batch() {
        CodeviewBean batchCodes = mSubBLir.getBatchCodes();
        if (mLastCheckView != null) {
            mLastCheckView.setChecked(false);
            mLastCheckView = null;
        }
        if (batchCodes != null) {
            IOTBatchInterface.IOTBlirViewCheckInterface iotBlirViewChectInterface = mBlIrViews.get(batchCodes.KeyType);
            if (iotBlirViewChectInterface != null) {
                iotBlirViewChectInterface.setChecked(true);
            }
        }
    }

    private void updateViewSelected(IOTSubBLir newDev) {
        HashMap<String, CodeviewBean> irCode = newDev.getIRCode();
        String newDevType = newDev.getDevType();
        if (BlirConst.IR_SUBDEV_TYPE_PLAYER.equals(newDevType)) {
            if (mSubBLir == null || !BlirConst.IR_SUBDEV_TYPE_PLAYER.equals(mSubBLir.getDevType())) {
                mLlDvdControl.setVisibility(View.VISIBLE);
                if (mLeftPart.getVisibility() != View.GONE) {
                    mLeftPart.setVisibility(View.GONE);
                    mRightPart.setVisibility(View.GONE);
                }
                if (mFanContainer.getVisibility() != View.GONE) {
                    mFanContainer.setVisibility(View.GONE);
                }
                if (!mCheckOnline) {
                    mIvDvdPlay.setPadding(mPadding, mPadding, mPadding, mPadding);
                    mIvDvdPause.setPadding(mPadding, mPadding, mPadding, mPadding);
                    mIvDvdStop.setPadding(mPadding, mPadding, mPadding, mPadding);
                }
                mIvControlTop.setTag(BlirConst.IR_KEYTYPE_BACK);
                mIvControlLeft.setTag(BlirConst.IR_KEYTYPE_BACKWARD);
                mIvControlRight.setTag(BlirConst.IR_KEYTYPE_FORWARD);
                mIvControlBottom.setTag(BlirConst.IR_KEYTYPE_NEXT);
                mIvControlNumber.setTag(BlirConst.IR_KEYTYPE_OPEN);

                mIvControlTop.setImageResource(R.drawable.icon_speed);
                mIvControlLeft.setImageResource(R.drawable.icon_previous);
                mIvControlRight.setImageResource(R.drawable.icon_next);
                mIvControlBottom.setImageResource(R.drawable.icon_rewind);
                mIvControlNumber.setImageResource(R.drawable.icon_withdraw);
            }
            mIvControlNumber.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mIvControlNumber.getTag()) == null);
            mIvDvdPlay.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mIvDvdPlay.getTag()) == null);
            mIvDvdPause.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mIvDvdPause.getTag()) == null);
            mIvDvdStop.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mIvDvdStop.getTag()) == null);

        } else if (BlirConst.IR_SUBDEV_TYPE_FAN.equals(newDevType)) {
            if (mSubBLir == null || !BlirConst.IR_SUBDEV_TYPE_FAN.equals(mSubBLir.getDevType())) {
                if (mFanContainer.getVisibility() != View.VISIBLE) {
                    mFanContainer.setVisibility(View.VISIBLE);
                }
                if (mLlDvdControl.getVisibility() != View.GONE) {
                    mLlDvdControl.setVisibility(View.GONE);
                }
                if (mHeartBottomPart.getVisibility() != View.GONE) {
                    mHeartBottomPart.setVisibility(View.GONE);
                }
                if (mLeftPart.getVisibility() != View.GONE) {
                    mLeftPart.setVisibility(View.GONE);
                    mRightPart.setVisibility(View.GONE);
                }
                if (mLlNormalDevControlView.getVisibility() != View.GONE) {
                    mLlNormalDevControlView.setVisibility(View.GONE);
                }
            }

            mFanDamper.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mFanDamper.getTag()) == null);
            mFanHead.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mFanHead.getTag()) == null);
            mFanMode.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mFanMode.getTag()) == null);
            mFanTiming.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mFanTiming.getTag()) == null);

        } else {
            if(mLlNormalDevControlView.getVisibility() != View.VISIBLE){
                mLlNormalDevControlView.setVisibility(View.VISIBLE);
            }
            if (mSubBLir != null && BlirConst.IR_SUBDEV_TYPE_PLAYER.equals(mSubBLir.getDevType())) {
                mLlDvdControl.setVisibility(View.GONE);
                mIvControlTop.setTag(BlirConst.IR_KEYTYPE_UP);
                mIvControlLeft.setTag(BlirConst.IR_KEYTYPE_LEFT);
                mIvControlRight.setTag(BlirConst.IR_KEYTYPE_RIGHT);
                mIvControlBottom.setTag(BlirConst.IR_KEYTYPE_DOWN);
                mIvControlNumber.setTag(null);
                mIvControlNumber.setSelected(false);

                mIvControlTop.setImageResource(R.drawable.icon_channel_plus);
                mIvControlLeft.setImageResource(R.drawable.icon_voice_minus);
                mIvControlRight.setImageResource(R.drawable.icon_voice_plus);
                mIvControlBottom.setImageResource(R.drawable.icon_channel_minus);
                mIvControlNumber.setImageResource(R.drawable.icon_number1);
            }

            //            if (BlirConst.IR_SUBDEV_TYPE_WATER_HEATER.equals(newDevType)) {
            //                if (mHeartBottomPart.getVisibility() != View.VISIBLE) {
            //                    mHeartBottomPart.setVisibility(View.VISIBLE);
            //                }
            //                mHeartPartLeft.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mHeartPartLeft.getTag()) == null);
            //                mHeartPartRight.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mHeartPartRight.getTag()) == null);
            //            } else {
            //                if (mHeartBottomPart.getVisibility() != View.GONE) {
            //                    mHeartBottomPart.setVisibility(View.GONE);
            //                }
            //            }

            if (mFanContainer.getVisibility() != View.GONE) {
                mFanContainer.setVisibility(View.GONE);
            }

            if (BlirConst.IR_SUBDEV_TYPE_AMPLIFIER.equals(newDevType)) {
                if (mLeftPart.getVisibility() != View.GONE) {
                    mLeftPart.setVisibility(View.GONE);
                    mRightPart.setVisibility(View.GONE);
                }

            } else {
                if (mLeftPart.getVisibility() != View.VISIBLE) {
                    mLeftPart.setVisibility(View.VISIBLE);
                    mRightPart.setVisibility(View.VISIBLE);
                }

                if (BlirConst.IR_SUBDEV_TYPE_PROJECTOR.equals(newDevType) && (mSubBLir == null || !BlirConst.IR_SUBDEV_TYPE_PROJECTOR.equals(mSubBLir.getDevType()))) {
                    mRightPartTv.setText(R.string.zoom);
                    mRightPartTop.setImageResource(R.drawable.icon_enlarge);
                    mRightPartBottom.setImageResource(R.drawable.icon_narrow);
                    mRightPartTop.setTag(BlirConst.IR_KEYTYPE_ZOOMIN);
                    mRightPartBottom.setTag(BlirConst.IR_KEYTYPE_ZOOMOUT);
                } else if (!BlirConst.IR_SUBDEV_TYPE_PROJECTOR.equals(newDevType)) {
                    mRightPartTv.setText(R.string.tv_channel);
                    mRightPartTop.setImageResource(R.drawable.icon_plus);
                    mRightPartBottom.setImageResource(R.drawable.icon_reduce);
                    mRightPartTop.setTag(BlirConst.IR_KEYTYPE_CH_ADD);
                    mRightPartBottom.setTag(BlirConst.IR_KEYTYPE_CH_RDEDUCE);
                }

                mLeftPartTop.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mLeftPartTop.getTag()) == null);
                mLeftPartBottom.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mLeftPartBottom.getTag()) == null);
                mRightPartTop.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mRightPartTop.getTag()) == null);
                mRightPartBottom.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mRightPartBottom.getTag()) == null);

            }
        }

        mIvControlMore.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mIvControlMore.getTag()) == null);
        mIvControlTop.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mIvControlTop.getTag()) == null);
        mIvControlSound.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mIvControlSound.getTag()) == null);
        mIvControlLeft.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mIvControlLeft.getTag()) == null);
        mIvControlOk.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mIvControlOk.getTag()) == null);
        mIvControlRight.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mIvControlRight.getTag()) == null);
        mIvControlBottom.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mIvControlBottom.getTag()) == null);
        mIvControlReturn.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mIvControlReturn.getTag()) == null);
        mIvControlToggle.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mIvControlToggle.getTag()) == null);

    }

    private void updateNumberSelected(IOTSubBLir newDev) {
        if (newDev != null) {
            HashMap<String, CodeviewBean> irCode = newDev.getIRCode();
            mNumb0.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mNumb0.getTag()) == null);
            mNumb1.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mNumb1.getTag()) == null);
            mNumb2.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mNumb2.getTag()) == null);
            mNumb3.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mNumb3.getTag()) == null);
            mNumb4.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mNumb4.getTag()) == null);
            mNumb5.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mNumb5.getTag()) == null);
            mNumb6.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mNumb6.getTag()) == null);
            mNumb7.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mNumb7.getTag()) == null);
            mNumb8.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mNumb8.getTag()) == null);
            mNumb9.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mNumb9.getTag()) == null);
            mNumbOther.setSelected((irCode == null || irCode.size() <= 0) || irCode.get(mNumbOther.getTag()) == null);
        }
    }

    private boolean checkOnline() {
        if (!mSubBLir.getRealOnline()) {
            int state = BLLet.Controller.queryDeviceState(mSubBLir.getDid());
            if (state == BLDeviceState.OFFLINE) {
                mOfflineMask.setVisibility(View.VISIBLE);
                mSubBLir.setRealOnline(false);
                return false;
            }
            mSubBLir.setRealOnline(true);
        }
        if (mOfflineMask.getVisibility() != View.GONE) {
            mOfflineMask.setVisibility(View.GONE);
        }
        return true;
    }

}
